<template>
  <div>
    <introduce>
      <span class="title-gray" slot="pre">安全/</span>
      <span class="title-bold" slot="title"> API访问密钥</span>
      <span slot="info">
        - 使用访问密钥向SenseCAP API服务发出安全的REST或HTTP请求。</span
      >
    </introduce>
    <el-table :data="securityList" stripe style="width: 100%; margin-top: 10px">
      <el-table-column type="index" width="50" label="序号"> </el-table-column>
      <el-table-column prop="tag" label="标签" width="180"> </el-table-column>
      <el-table-column prop="id" label="密钥ID" width="180"> </el-table-column>
      <el-table-column prop="authority" label="访问权限"> </el-table-column>
      <el-table-column label="密钥状态" prop="online">
        <template slot-scope="scope">
          {{ scope.row.online ? "使用中" : "未使用" }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间">
        <template slot-scope="scope">
          {{ scope.row.cTime | dateformat }}
        </template>
      </el-table-column>
      <el-table-column label="更新时间">
        <template slot-scope="scope">
          {{ scope.row.uTime | dateformat }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            @click="handleEdit(scope.row)"
            type="primary"
            icon="el-icon-edit"
            circle
          ></el-button>
          <el-button
            @click="remove(scope.row)"
            type="danger"
            icon="el-icon-delete"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="修改密钥信息" :visible.sync="dialogVisible" width="30%">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
      >
        <el-form-item label="标签" prop="tag">
          <el-input v-model="ruleForm.tag"></el-input>
        </el-form-item>
        <el-form-item label="密钥ID" prop="id">
          <el-input v-model="ruleForm.id" disabled></el-input>
        </el-form-item>
        <el-form-item label="访问权限" prop="authority">
          <el-input v-model="ruleForm.authority"></el-input>
        </el-form-item>
        <el-form-item label="密钥状态" prop="online">
          <el-select v-model="ruleForm.online">
            <el-option label="未使用" :value="false"></el-option>
            <el-option label="使用中" :value="true"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="edit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Introduce from "@/components/Introduce.vue";
import { $securityList, $securityDel, $securityEdit } from "@/api/index";
export default {
  name: "securityList",
  components: { Introduce },
  data() {
    return {
      securityList: [],
      dialogVisible: false,
      ruleForm: {
        tag: "",
        id: "",
        online: false,
        authority: "",
      },
      rules: {},
    };
  },
  mounted() {
    this.getSecurityList();
  },
  methods: {
    edit() {
      $securityEdit(this.ruleForm).then((res) => {
        if (res.data.msg == "success") {
          this.dialogVisible = false;
        }
      });
      this.getSecurityList();
    },
    handleEdit(row) {
      this.dialogVisible = true;
      this.ruleForm = row;
    },
    getSecurityList() {
      $securityList().then((res) => {
        console.log(res);
        this.securityList = res.data.data;
      });
    },
    remove(row) {
      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          $securityDel({ id: row.id }).then((res) => {
            if (res.data.msg == "success") {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getSecurityList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  filters: {
    dateformat(val) {
      let t = new Date(+val);
      let y = t.getFullYear();
      let m = (t.getMonth() + 1 + "").padStart(2, "0");
      let d = (t.getDate() + "").padStart(2, "0");
      return `${y}-${m}-${d}`;
    },
  },
};
</script>

<style>
</style>